package com.steven.door.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.example.x6.serial.SerialUtil;
import com.steven.door.R;
import com.steven.door.base.BaseActivity;
import com.steven.door.services.WebSocketService;
import com.steven.door.utils.NetCheckUtil;
import com.steven.door.utils.ToastUtils;

import java.lang.ref.WeakReference;

public class SplashActivity extends BaseActivity {
    private MyHandler myHandler;

    @Override
    public int intiLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void intiView() {

    }

    @Override
    public void intiData() {
        myHandler=new MyHandler(this);
        if (NetCheckUtil.checkNet(this)) {
            SerialUtil.getInstance().openSerialPort();//打开串口
            startService(new Intent(this, WebSocketService.class));//启动服务 链接socket
            myHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            ToastUtils.showToast(this,"请先配置网络~");
        }
    }

    //静态内部类handler，防止内存泄漏
    private static class MyHandler extends Handler {

        private WeakReference<SplashActivity> mActivity;

        private MyHandler(SplashActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity act = mActivity.get();
            if (act != null) {
                //引导页去掉
                act.startActivity(new Intent(act, MainActivity.class));
                act.finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myHandler != null) {
            myHandler = null;
        }
    }
}
